package com.xpsoft.oa.service.admin.impl;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.xpsoft.core.service.impl.BaseServiceImpl;
import com.xpsoft.oa.dao.admin.AdminContractDao;
import com.xpsoft.oa.dao.admin.AdminContractPaymentDao;
import com.xpsoft.oa.model.admin.AdminContract;
import com.xpsoft.oa.model.admin.AdminContractPayment;
import com.xpsoft.oa.service.admin.AdminContractPaymentService;

public class AdminContractPaymentServiceImpl extends BaseServiceImpl<AdminContractPayment> implements AdminContractPaymentService{
	private AdminContractPaymentDao dao;
	@Autowired
	private AdminContractDao adminContractDao;
	public AdminContractPaymentServiceImpl(AdminContractPaymentDao dao) {
		super(dao);
		this.dao=dao;
	}

	@Override
	public Map<String, Object> getPayed(String adminContractId) {
		// TODO Auto-generated method stub
		Map<String,Object> map = new HashMap<String,Object>();
		if(StringUtils.isNotBlank(adminContractId)){
			Long contractId = Long.valueOf(adminContractId);
			AdminContract adminContract = adminContractDao.get(Long.valueOf(contractId));
			BigDecimal contractAmount = adminContract.getContractAmount();
			BigDecimal payedAmount = dao.getPayedByContractId(contractId);
			BigDecimal payedPercentage = payedAmount.multiply(new BigDecimal(100)).divide(contractAmount,2,RoundingMode.HALF_DOWN);
			map.put("payedPercentage", payedPercentage);
			map.put("payedAmount", payedAmount);
		}else{
			map.put("payedPercentage", 0);
			map.put("payedAmount", 0);
		}
		return map;
	}

	
}